package com.example.backend.utils;

import com.baomidou.mybatisplus.annotation.TableField;
import com.example.backend.enums.ResultCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;

import java.io.Serializable;

/**
 * 通用返回结构类
 *
 * @author mhwssad
 */
@Getter
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(description = "通用返回结构类")
public class ResultJson<T> implements Serializable {
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
    private static final ObjectMapper objectMapper = new ObjectMapper();
    /**
     * 状态码
     */
    @Schema(description = "状态码")
    private int code;
    /**
     * 描述信息
     */
    @Schema(description = "描述信息")
    private String msg;
    /**
     * 返回数据
     */
    @Schema(description = "返回数据")
    private T data;

    /**
     * 构建响应结果
     */
    public static <T> ResultJson<T> of(int code, String msg, T data) {
        return ResultJson.<T>builder().code(code).msg(msg).data(data).build();
    }

    /**
     * 成功响应（带数据）
     */
    public static <T> ResultJson<T> success(T data) {
        return of(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 成功响应（无数据）
     */
    public static <T> ResultJson<T> ok() {
        return success(null);
    }

    /**
     * 错误响应（自定义错误码）
     */
    public static <T> ResultJson<T> error(ResultCodeEnum errorCode) {
        return of(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 自定义错误信息
     */
    public static <T> ResultJson<T> error(int code, String msg) {
        return of(code, msg, null);
    }

    public static ResultJson<?> fail(String msg) {
        return error(ResultCodeEnum.INTERNAL_SERVER_ERROR.getCode(), msg);
    }

    public static <T> ResultJson<T> fail(int code, String msg) {
        return of(code, msg, null);
    }

    /**
     * 将 ResultJson 对象序列化为 JSON 字符串
     *
     * @param result ResultJson 对象
     * @return JSON 字符串
     */
    public static String serialize(ResultJson<?> result) {
        try {
            return objectMapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("序列化失败", e);
        }
    }

    /**
     * 将 JSON 字符串反序列化为 ResultJson<T> 对象
     *
     * @param json     JSON 字符串
     * @param dataType 数据类型
     * @return ResultJson<T> 对象
     */
    public static <T> ResultJson<T> deserialize(String json, Class<T> dataType) {
        try {
            return objectMapper.readValue(json, objectMapper.getTypeFactory().constructParametricType(ResultJson.class, dataType));
        } catch (JsonProcessingException e) {
            throw new RuntimeException("反序列化失败", e);
        }
    }
}

